package com.handpay.nocardpay.baselibrary.widget;

import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;

/**
 * Created by sxshi on 2017-12-8.
 */

public class DragFloatingButton extends AppCompatButton {
    private int screenWidth;
    private int screenHeight;
    private int screenWidthHalf;
    private int statusHeight;
    private int navigationBarHeight;

    public DragFloatingButton(Context context) {
        this(context, null);
    }

    public DragFloatingButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragFloatingButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        float bottomBarHeight = getContext().getResources().getDimension(R.dimen.bottombar_margin_height);
//        HPLog.i(HPLog.SSX_TAG, "底部BottomBar的高度:" + bottomBarHeight);
        screenWidth = DisplayUtil.getScreenWidth(getContext());
//        HPLog.i(HPLog.SSX_TAG, "屏幕宽度:" + screenWidth);
        screenWidthHalf = screenWidth / 2;
        screenHeight = DisplayUtil.getScreenHeight(getContext());
//        HPLog.i(HPLog.SSX_TAG, "屏幕高度度:" + screenHeight);
        statusHeight = DisplayUtil.getStatusHeight(getContext());
//        HPLog.i(HPLog.SSX_TAG, "状态栏高度:" + statusHeight);
        navigationBarHeight = DisplayUtil.getNavigationBarHeight(getContext());
//        HPLog.i(HPLog.SSX_TAG, "导航栏高度:" + navigationBarHeight);
        screenHeight = DisplayUtil.getScreenHeight(getContext()) - (int) bottomBarHeight - statusHeight;
//        HPLog.i(HPLog.SSX_TAG, "可以移动的距离=屏幕的总体高度-底部BottomBar的高度-状态栏高度" + screenHeight);
        setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
    }

    private int lastX;
    private int lastY;
    private boolean isDrag;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int rawX = (int) ev.getRawX();
        int rawY = (int) ev.getRawY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isDrag = false;
                getParent().requestDisallowInterceptTouchEvent(true);
                lastX = rawX;
                lastY = rawY;
                break;
            case MotionEvent.ACTION_MOVE:
                isDrag = true;
                //计算手指移动了多少
                int dx = rawX - lastX;
                int dy = rawY - lastY;
                int distance = (int) Math.sqrt(dx * dx + dy * dy);
                HPLog.i("distance---->", distance + "");
                if (distance < 3) {//给个容错范围，不然有部分手机还是无法点击
                    isDrag = false;
                    break;
                }

                float x = getX() + dx;
                float y = getY() + dy;

                //检测是否到达边缘 左上右下
                x = x < 0 ? 0 : x > screenWidth - getWidth() ? screenWidth - getWidth() : x;
                if (y < 0) {
                    y = 0;
                }
                if (y > screenHeight - getHeight()) {
                    y = screenHeight - getHeight();
                }
                setX(x);
                setY(y);

                lastX = rawX;
                lastY = rawY;
                break;
            case MotionEvent.ACTION_UP:
                //只吸附到右边屏幕
                animate().setInterpolator(new DecelerateInterpolator())
                        .setDuration(200)
                        .xBy(screenWidth - getWidth() - getX())
                        .start();
//                if (isDrag) {
//                    //恢复按压效果
//                    setPressed(false);
                //通过屏幕一半判断吸附到左边还是右边
//                    if (rawX > screenWidthHalf) {
//                        animate().setInterpolator(new DecelerateInterpolator())
//                                .setDuration(200)
//                                .xBy(screenWidth - getWidth() - getX())
//                                .start();
//                    } else {
//                        ObjectAnimator oa = ObjectAnimator.ofFloat(this, "x", getX(), 0);
//                        oa.setInterpolator(new DecelerateInterpolator());
//                        oa.setDuration(200);
//                        oa.start();
//                    }
//                }
                break;
        }
        return isDrag || super.onTouchEvent(ev);
    }
}
